package com.miracle.main.module.mvp.presenter;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.laojiang.imagepickers.utils.T;
import com.miracle.basemvp.base.BasePresenter;
import com.miracle.basemvp.base.BaseResponse;
import com.miracle.basemvp.base.IPresenter;
import com.miracle.basemvp.base.state.StateController;
import com.miracle.basemvp.lifecycle.rx.RxDefaultObserver;
import com.miracle.common.mvp.entity.response.BaseListResponse;
import com.miracle.common.util.CommonConstants;
import com.miracle.common.util.PreferencesUtils;
import com.miracle.main.module.mvp.contract.FileListContract;
import com.miracle.main.module.mvp.entity.bean.FileItem;
import com.miracle.main.module.mvp.entity.request.FileListRequest;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.util.ArrayList;

import javax.inject.Inject;

import static com.miracle.common.util.CheckUtil.checkListIsEmpty;
import static com.miracle.main.module.mvp.ui.fragment.FileListFragment.TYPE_PIC;

/**
 * com.miracle.main.module.mvp.presenter
 * (c)2018 AIR Times Inc. All rights reserved.
 *
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/7/11 10:18
 * @see com.miracle.main.module.mvp.presenter
 */
public class FileListPresenter extends BasePresenter<FileListContract.View, FileListContract
        .Model> implements IPresenter {

    @Inject
    public FileListPresenter(FileListContract.View view, FileListContract.Model model) {
        super(view, model);
    }

    StateController stateController;

    public void getFileList(LifecycleProvider<FragmentEvent> lifecycleProvider, int type, final
    StateController stateController) {
        this.stateController = stateController;
        Activity activity = ((Fragment) mView).getActivity();
        String jessionId = PreferencesUtils.getPreferences(activity, PreferencesUtils.State
                .USER_SESSIONID);
        FileListRequest request = new FileListRequest();
        request.setUri(CommonConstants.URI.URI_FILE_LIST);
        request.setJSESSIONID(jessionId);
        request.setPageNo(mView.getCurrPage());
        if (type == TYPE_PIC) {
            request.setFileType("photos");
        } else {
            request.setFileType("");
        }
        request.setPageSize(mView.getPageSize());

        mModel.getFileList(request)
                .compose(lifecycleProvider.<BaseResponse<BaseListResponse<FileItem>>>bindUntilEvent
                        (FragmentEvent.DESTROY))
                .subscribe(new RxDefaultObserver<BaseResponse<BaseListResponse<FileItem>>>
                        (stateController) {
                    @Override
                    public void onSucceed(BaseResponse<BaseListResponse<FileItem>>
                                                  arrayListBaseResponse) {
                        stateController.onSuccessState(mView, arrayListBaseResponse,
                                (arrayListBaseResponse.getContent() != null) ?
                                        arrayListBaseResponse.getContent().getList() : null);
                    }

                });
    }

    private int gloable;

    private void test() {
        int gloable = 2;
        this.gloable = 3;
    }

}
